import api from '../../../api/index';
import { saveUserAndStorage } from '../../../config/userUtil'
Page({

  data: {
    getdata: {},
    show: false,
    userInfo: null,
    weight: null,
    height: 170,
    styles: {
      line: '#dbdbdb',
      bginner: '#fbfbfb',
      bgoutside: '#ffffff',
      font: '#404040',
      fontColor: '#6E6E6E',
      fontSize: 16
    }
  },
  onLoad(options) {
    console.log('onLoad')
    this.userload()
    this.get()
  },
  getuser(userInfo) {
    if (userInfo) {
      console.log("userInfo 缓存值为：", userInfo);
      wx.setStorageSync('userInfo', userInfo)
      if(userInfo.height) {
        this.setData({
          height: parseFloat(userInfo.height)
        });
      }
      if(userInfo.weight) {
        this.setData({
          weight: parseFloat(userInfo.weight)
        });
      }
      this.setData({
        show: true
      });
    } else {
      console.log("userInfo 未找到相应缓存");
      this.setData({
        show: true
      });
    }
  },
  // 查询有什么
  async get() {
    try {
      const res = await api.post('/api/user/storage/get', {
        data: {
          'key': 'hxhealthsporttags'
        }
      })
      let array = res[0].value
      console.log(array);
      // value有东西
      if (array) {
        try {
          array = JSON.parse(array);
          this.setData({
            getdata: array
          });
          console.log("字符串包含对象:", array);
          if(!array.sporttagsfirst) {
             console.log('没有sporttagsfirst字段,添加sporttagsfirst字段');
             array.sporttagsfirst = 'unonce'
             this.setData({
              getdata: array,
              result: {sporttagsfirst: 'unonce'}
            });
            this.putData()
          } 
        } catch (error) {
          console.log("value是纯文本,添加sporttagsfirst字段", array);
          this.setData({
            getdata: {sporttagsfirst: 'unonce'}
          });
          this.putData()
        wx.hideLoading()
        }
      } else {
        // 没有第一次进入的字段
        this.setData({
          getdata: {sporttagsfirst: 'unonce'}
        });
        this.putData()
        wx.hideLoading()
      }
    } catch (e) {
      console.log('get->', e)
      return null
    }
  },
  async putData() {
    let getdata = this.data.getdata
    console.log(getdata);
    try {
      const res = await api.post('/api/user/storage/set', {
        data: {
          'key': 'hxhealthsporttags',
          'value': JSON.stringify(getdata)
        }
      })
      console.log(res);
      if (res) {
        console.log('保存字段成功');
      }
    } catch (e) {
      console.log('put->', e)
      return null
    }
  },
  async goNext() {
    let userInfo = this.data.userInfo
    const height = this.data.height
    const weight = this.data.weight
    if(height && weight) {
      userInfo.height = height
      userInfo.weight = weight
      const flag = await saveUserAndStorage(this.data.userInfo)
      console.log('baocun');
      if(!flag) { 
        return 
      } else {
        console.log('22222······');
        this.userload()
        wx.navigateTo({ url: '/sportsClass/pages/body/index' });
        // wx.reLaunch ({ url: '/sportsClass/pages/body/index' });
      }
      console.log('成功了----');
    } else {
      wx.showToast({
        title: '请填写您的身高！',
        icon: 'none',
        duration: 1000
      })
    }
  },
  userload() {
    console.log('21222');
    wx.setStorageSync('userInfo', '');
    // 整体列表查询
    api.post('/api/hxhealth/user/load').then((res) => {
      // console.log(res);
      console.log('----res----', res[0])
      
      this.setData({
        userInfo: res[0]
      });
      this.getuser(res[0])
     
    })
  },
  bindvalue(e) { //滑动回调
    const value = e.detail.value;
    const key = e.currentTarget.id;
    console.log(key);
    const data = {};
    data[key] = value;
    this.setData(data);
    console.log(this.data);
  }
})